package com.plan.controller.card;

import com.plan.base.constant.BaseCode;
import com.plan.base.constant.Constant;
import com.plan.base.entity.Result;
import com.plan.controller.card.request.BaseReq;
import com.plan.controller.card.request.DeleteAwardProductReq;
import com.plan.controller.card.request.MyAwardPageListReq;
import com.plan.controller.card.request.SavePrizeDrawResultReq;
import com.plan.controller.card.response.AwardProductListRes;
import com.plan.controller.card.response.PrizeDrawRes;
import com.plan.controller.user.response.UserVO;
import com.plan.entity.card.AwardProduct;
import com.plan.entity.card.PrizeDrawLog;
import com.plan.service.card.AwardService;
import com.plan.service.card.TeamService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("award/")
public class AwardController {
    @Resource
    private TeamService teamService;
    @Resource
    private AwardService awardService;

    /**
     * 添加抽奖商品
     */
    @PostMapping(value = "addAwardProduct")
    public Result<Long> add(@RequestBody AwardProduct product, HttpServletRequest httpRequest) {
        if (product.getRate() == null || product.getRate() >= 100 || product.getRate() <= 0
            || product.getName() == null || product.getTeamId() == null) {
            return Result.fail(BaseCode.PARAM_ERROR);
        }
        UserVO vo = (UserVO) httpRequest.getAttribute(Constant.USER_KEY);
        if (!teamService.isUserInTeam(product.getTeamId(), vo.getId())) {
            return Result.fail(BaseCode.ILLEGAL_OPERATION);
        }
        product.setCreatorId(vo.getId());
        product.setUpdaterId(vo.getId());
        return awardService.insert(product);
    }

    /**
     * 修改抽奖商品
     */
    @PostMapping(value = "updateAwardProduct")
    public Result<Long> updateAwardProduct(@RequestBody AwardProduct product, HttpServletRequest httpRequest) {
        if (product.getId() == null || product.getRate() == null || product.getRate() > 100
            || product.getName() == null || product.getTeamId() == null) {
            return Result.fail(BaseCode.PARAM_ERROR);
        }
        UserVO vo = (UserVO) httpRequest.getAttribute(Constant.USER_KEY);
        if (!teamService.isUserInTeam(product.getTeamId(), vo.getId())) {
            return Result.fail(BaseCode.ILLEGAL_OPERATION);
        }
        product.setCreatorId(vo.getId());
        product.setUpdaterId(vo.getId());
        return awardService.update(product);
    }


    @PostMapping(value = "deleteAwardProduct")
    public Result<Long> deleteAwardProduct(@RequestBody DeleteAwardProductReq req, HttpServletRequest httpRequest) {
        if (req.getProductId() == null || req.getTeamId() == null) {
            return Result.fail(BaseCode.PARAM_ERROR);
        }
        UserVO vo = (UserVO) httpRequest.getAttribute(Constant.USER_KEY);
        if (!teamService.isUserInTeam(req.getTeamId(), vo.getId())) {
            return Result.fail(BaseCode.ILLEGAL_OPERATION);
        }
        awardService.delete(req.getTeamId(), req.getProductId());
        return Result.ok();
    }

    /**
     * 抽奖商品列表
     */
    @PostMapping(value = "awardProductList")
    public Result<List<AwardProductListRes>> awardProductList(@RequestBody BaseReq req, HttpServletRequest httpRequest) {
        if (req.getTeamId() == null) {
            return Result.fail(BaseCode.PARAM_ERROR);
        }
        UserVO vo = (UserVO) httpRequest.getAttribute(Constant.USER_KEY);
        if (!teamService.isUserInTeam(req.getTeamId(), vo.getId())) {
            return Result.fail(BaseCode.ILLEGAL_OPERATION);
        }

        List<AwardProductListRes> list = awardService.getAwardProductList(req.getTeamId());
        return Result.ok(list);
    }

    /**
     * 抽奖-后端抽奖的方式
     * 校验积分是否满足抽奖条件、生成我的奖品、积分扣减、生成积分扣减日志、生成抽奖记录
     */
    @PostMapping(value = "prizeDraw")
    public Result<PrizeDrawRes> prizeDraw(@RequestBody BaseReq req, HttpServletRequest httpRequest) {
        if (req.getTeamId() == null || req.getCardId() == null) {
            return Result.fail(BaseCode.PARAM_ERROR);
        }
        UserVO vo = (UserVO) httpRequest.getAttribute(Constant.USER_KEY);
        if (!teamService.isUserInTeam(req.getTeamId(), vo.getId())) {
            return Result.fail(BaseCode.ILLEGAL_OPERATION);
        }

        return awardService.prizeDraw(req, vo);
    }

    /**
     * 保存前端抽奖结果
     */
    @PostMapping(value = "savePrizeDrawResult")
    public Result<Long> savePrizeDrawResult(@RequestBody SavePrizeDrawResultReq req, HttpServletRequest httpRequest) {
        if (req.getTeamId() == null || req.getCardId() == null || req.getName() == null || req.getSpendPoint() == null) {
            return Result.fail(BaseCode.PARAM_ERROR);
        }
        UserVO vo = (UserVO) httpRequest.getAttribute(Constant.USER_KEY);
        if (!teamService.isUserInTeam(req.getTeamId(), vo.getId())) {
            return Result.fail(BaseCode.ILLEGAL_OPERATION);
        }
        req.setUpdaterId(vo.getId());

        return awardService.savePrizeDrawResult(req);
    }

    /**
     * 我的奖品
     */
    @PostMapping(value = "myAwardList")
    public Result<List<PrizeDrawLog>> myAwardList(@RequestBody MyAwardPageListReq req, HttpServletRequest httpRequest) {
        if (req.getTeamId() == null || req.getCardId() == null) {
            return Result.fail(BaseCode.PARAM_ERROR);
        }
        if (req.getPageSize() > Constant.CLIENT_MAX_PAGE_SIZE) {
            req.setPageSize(Constant.CLIENT_MAX_PAGE_SIZE);
        }
        UserVO vo = (UserVO) httpRequest.getAttribute(Constant.USER_KEY);
        if (!teamService.isUserInTeam(req.getTeamId(), vo.getId())) {
            return Result.fail(BaseCode.ILLEGAL_OPERATION);
        }

        return awardService.myAwardList(req);
    }

}
